#!/bin/bash
#
# Copyright (C) 2013 CIeNET, all rights reserved
#
# This is internal script. Don't use it unless you know what you are doing!!!

# Prints error message and exits
#source ~/.bash_profile

function log_s()
{
	echo -e "`date '+%H:%M:%S'-`$1"|tee -a  $AUTODEPLOY_LOG_FILE
}
function fail()
{
    log_s "$1" 
	log_s "operation faild, check the log and try again please."
	exit -1
} 
# Exits if given directory doesn't exist
function check_dir()
{
    [ -d $1 ] || fail "Directory $1 doesn't exist!";
}

function usage()
{
	echo -e "Usage: ./autoDeploy.sh  <update|compile|package|deploy|status> <PR|UT> [Sandglass|...]"
    exit -1
}

#log_s "`date '+%Y-%m-%d %H:%M:%S'`"
export  AUTODEPLOY_LOG_FILE="$AUTODEPLOY_HOME/logs/log4deploy.log" 
[ -f $AUTODEPLOY_LOG_FILE ] && mv "$AUTODEPLOY_LOG_FILE" "$AUTODEPLOY_LOG_FILE.0"

export  M2_HOME="/home/sgut/autobuild/apache-maven/apache-maven-3.0.4" || check_dir $M2_HOME
export  GIT_REPO="/home/sgut/autobuild/gitRepo/ccwp" || check_dir $GIT_REPO
export	GIT_PATH="git@10.80.1.44:ccwp.git"

# main
export ENV_FLAG=`echo $2| tr A-Z a-z`;

case $ENV_FLAG in
    ut)
		export  AUTODEPLOY_TOMCAT_HOME="/home/sgut/tomcat7" || check_dir $AUTODEPLOY_TOMCAT_HOME
		export  AUTODEPLOY_APP_TEST_URL="http://localhost:8899/Sandglass"
		;;
    pr)
		export  AUTODEPLOY_TOMCAT_HOME="/home/sandglass/tomcat/apache-tomcat-7.0.34" || check_dir $AUTODEPLOY_TOMCAT_HOME
		export  AUTODEPLOY_APP_TEST_URL="http://localhost:8888/Sandglass"
        ;;
    *)
        usage
        ;;
esac



